package mamma.action;

import static org.apache.struts2.StrutsStatics.HTTP_REQUEST;
import static org.apache.struts2.StrutsStatics.HTTP_RESPONSE;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mamma.common.PageNavigator;
import mamma.dao.AccountDao;
import mamma.dao.AdminDao;
import mamma.dao.BabyDao;
import mamma.dao.FriendsDao;
import mamma.dao.MemberDao;
import mamma.dao.PhotoDao;
import mamma.vo.BabyInfoVo;
import mamma.vo.FriendUpdateVo;
import mamma.vo.MemberVo;
import mamma.vo.PhotoVo;
import mamma.vo.VaccinationVo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class MemberAction extends ActionSupport {
	
	private MemberVo memberVo;					// the value object that has informations about the Member.
	private FriendUpdateVo friendVo;
	
	private List<PhotoVo> photoList;			// the list that include the user's photos.
	private List<BabyInfoVo> babyList;
	private List<FriendUpdateVo> friendList;
	
	private String email;						// the value that brings the user's info to the login page.
	private String password;					// the value that checks if the user's password is right.
	private String loginId;						// the value that has the login info.
	private String savedFile;
	private String name;
	
	private int page;							// the photos loaded by this value.
	private int friendId;
	private int lastPage;
	
	/*
	 * MEMBER ACTION
	 */
	
	// check the first access of the computer
	public String firstAccess(){
		System.out.println("FIRST ACCESS");
		ActionContext context = ActionContext.getContext();
		HttpServletRequest request = null;
		request = (HttpServletRequest)context.get(HTTP_REQUEST);
		Cookie[] cookie = request.getCookies();
		if(cookie != null){
			for(int i = 0; i < cookie.length; i++){
				if(cookie[i].getName().equals("firstAccess")){
					// if the computer has the cookie named "firstAccess", return "SUCCESS".
					// it means that the computer connected this site in the past.
					return SUCCESS;
				}
			}
			Cookie firstAccess = new Cookie("firstAccess", "true");
			firstAccess.setMaxAge(365*24*60*60);
			HttpServletResponse response = null;
			response = (HttpServletResponse)context.get(HTTP_RESPONSE);
			response.addCookie(firstAccess);
			
			return INPUT;
		}
		else{
			Cookie firstAccess = new Cookie("firstAccess", "true");
			firstAccess.setMaxAge(365*24*60*60);
			HttpServletResponse response = null;
			response = (HttpServletResponse)context.get(HTTP_RESPONSE);
			response.addCookie(firstAccess);
		}
		// this return named "INPUT" means the first access.
		return INPUT;
	}
	
	// search the email on our database
	public String emailCheck(){
		MemberDao dao = new MemberDao();
		boolean result = dao.emailCheck(email);
		// true of 'result' means that our database doesn't have this email.
		if(result == false){
			return ERROR;
		}
		return SUCCESS;
	}
	
	// login check
	public String loginCheck(){
		// if the user is logging in, just let him/her go the user's home.
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		memberVo = (MemberVo)session.get("login");
		
		return SUCCESS;
	}
	
	// insert the account
	public String insertAccount(){
		System.out.println("insertAccount method activated");
		MemberDao dao = new MemberDao();
		memberVo.setEmail(memberVo.getEmail().trim());
		memberVo.setPassword(memberVo.getPassword().trim());
		
		dao.insertAccount(memberVo);
		System.out.println(1);
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		System.out.println(session);
		System.out.println(memberVo);
		session.put("login", memberVo);
		
		System.out.println("insertAccount method end");
		return SUCCESS;
	}
	
	// login
	public String login(){
		memberVo = new MemberVo();
		memberVo.setEmail(email);
		memberVo.setPassword(password);
		
		MemberDao dao = new MemberDao();
		MemberVo temp = dao.selectAccount(memberVo);
		
		
		if(temp == null){
			return LOGIN;
		}
		memberVo = temp;
		
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		session.put("login", memberVo);
		
		return SUCCESS;
	}
	
	// logout
	public String logout(){
		System.out.println("logout activated");
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		session.clear();
		System.out.println("logout end");
		return SUCCESS;
	}
	
	// go the user's home
	public String goHome(){
		System.out.println(email);
		memberVo = new MemberVo();
		memberVo.setEmail(email);
		MemberDao dao = new MemberDao();
		memberVo = dao.selectAccount(memberVo);
		
		// memberVo has a user's home info that the user logging in now accessed.
		page = 1;
		selectPhoto();
		
		PhotoDao dao2 = new PhotoDao();
	    savedFile = dao2.gtProfile(memberVo.getProfile_id());
	    int total = dao2.totalPhoto(email);
	    
	    PageNavigator pn = new PageNavigator(20, 5, 1, total);
	    lastPage = pn.getEndPageGroup();
	    System.out.println(lastPage);
	    BabyDao dao3 = new BabyDao();
	    BabyInfoVo babyInfoVo = new BabyInfoVo();
	    babyInfoVo.setMemberemail(email);
	    babyList = dao3.selectBabyList(babyInfoVo);
	    
	    if(babyList.size() == 0){
	    	babyList.add(babyInfoVo);
	    }
	    
		// loading the user who is loggin in now.
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		MemberVo temp = (MemberVo)session.get("login");
		if(temp != null){
			loginId = temp.getEmail();
			if(!loginId.equals(memberVo.getEmail())){
				FriendsAction friendsAction = new FriendsAction();
				friendVo = friendsAction.checkRequest(email, loginId);
				return "other";
			}
		}
		return SUCCESS;
	}
	
	public String goFriendPage(){
		System.out.println("goFriendPage method activated");
		
		FriendsDao dao = new FriendsDao();
		friendList = dao.goFriendPage(email);
		
		System.out.println("goFriendPage method end");
		return SUCCESS;
	}
	
	/*
	 * ACCOUNT ACTION
	 */
	
	// load the user's photos
	public String selectPhoto(){
		PhotoDao dao = new PhotoDao();
		photoList = dao.photoList(email, 0, 20);
		
		return SUCCESS;
	}
	
	// load one photo
	public String loadPhoto(){
		System.out.println("loadPhoto method activated");
		return SUCCESS;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public MemberVo getMemberVo() {
		return memberVo;
	}

	public void setMemberVo(MemberVo memberVo) {
		this.memberVo = memberVo;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getLoginId() {
		return loginId;
	}

	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	public String getSavedFile() {
		return savedFile;
	}

	public void setSavedFile(String savedFile) {
		this.savedFile = savedFile;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getFriendId() {
		return friendId;
	}

	public void setFriendId(int friendId) {
		this.friendId = friendId;
	}

	public List<PhotoVo> getPhotoList() {
		return photoList;
	}

	public void setPhotoList(List<PhotoVo> photoList) {
		this.photoList = photoList;
	}

	public FriendUpdateVo getFriendVo() {
		return friendVo;
	}

	public void setFriendVo(FriendUpdateVo friendVo) {
		this.friendVo = friendVo;
	}

	public List<BabyInfoVo> getBabyList() {
		return babyList;
	}

	public void setBabyList(List<BabyInfoVo> babyList) {
		this.babyList = babyList;
	}

	public int getLastPage() {
		return lastPage;
	}

	public void setLastPage(int lastPage) {
		this.lastPage = lastPage;
	}

	public List<FriendUpdateVo> getFriendList() {
		return friendList;
	}

	public void setFriendList(List<FriendUpdateVo> friendList) {
		this.friendList = friendList;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}
